package com.hksj.em.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.common.core.util.EasyExcelUtil;
import com.hksj.em.mapper.AlarmInfoMapper;
import com.hksj.em.query.AlarmInfoQuery;
import com.hksj.em.vo.AlarmPropertyVO;
import com.hksj.em.vo.AlarmRankCountVO;
import com.hksj.em.vo.AlarmRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Null;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Component
public class AlarmInfoService {

    @Autowired
    private AlarmInfoMapper alarmInfoMapper;

    public IPage<AlarmRecordVO> queryAlarmRecord(AlarmInfoQuery query) {
        // 创建分页对象
        Page<AlarmRecordVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        List<AlarmRecordVO> list = alarmInfoMapper.queryAlarmRecord(page, query);
        // 设置分页结果
        page.setRecords(list);
        return page;
    }

    public List<AlarmRankCountVO> queryAlarmRankingList(@RequestBody AlarmInfoQuery query) {
        List<AlarmRankCountVO> list = alarmInfoMapper.queryAlarmRankingList(query);
        return list;
    }

    public List<AlarmPropertyVO> queryAlarmByProperty(@RequestBody AlarmInfoQuery query) {
        List<AlarmPropertyVO> list = alarmInfoMapper.queryAlarmByProperty(query);
        return list;
    }

    public void exportAlarmExcelData(AlarmInfoQuery query, HttpServletResponse response) {
        List<AlarmRecordVO> list = alarmInfoMapper.queryAlarmRecord(null, query);
        if (ObjectUtil.isNotNull(list)) {
            String fileName = new StringBuffer().append("手术室报警记录").append(new SimpleDateFormat("_yyyyMMddHHmmss").format(new Date())).toString();
            EasyExcelUtil.exportExcelData(response, fileName, list, AlarmRecordVO.class);
        } else {
            throw new RuntimeException("当前房间没有符合要求的数据，无需导出");
        }
    }
}
